package com.taotao.rop.service;

import com.rop.annotation.HttpAction;
import com.rop.annotation.NeedInSessionType;
import com.rop.annotation.ServiceMethod;
import com.rop.annotation.ServiceMethodBean;
import com.rop.session.SimpleSession;
import com.taotao.admin.MyRopClient;
import com.taotao.rop.database.domain.WechatArticle;
import com.taotao.rop.database.mapper.WechatArticleMapper;
import com.taotao.rop.manager.RopSessionManager;
import com.taotao.rop.response.BaseResponse;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@ServiceMethodBean(httpAction = HttpAction.POST, version = "1.0")
public class WechatArticleService {
    @Autowired
    private WechatArticleMapper wechatArticleMapper;

    @ServiceMethod(method = "wechatArticle.list", needInSession = NeedInSessionType.NO)
    public Object listArticles(WechatArticle request) {
        WechatArticle condition = new WechatArticle();
        condition.setParentId(0L);
        String appId = ((SimpleSession)request.getRopRequestContext().getSession()).getAttribute(RopSessionManager.KEY_APP_ID).toString();
        if (!appId.equalsIgnoreCase(MyRopClient.APP_ID)) {
            condition.setAppId(appId);
        }
        List<WechatArticle> parentList = wechatArticleMapper.getList(condition);
        List<WechatArticle> theParentList = new ArrayList<WechatArticle>();
        for (WechatArticle parent : parentList) {
            condition.setParentId(parent.getId());
            List<WechatArticle> subList = wechatArticleMapper.getList(condition);
            parent.setSubArticleList(subList);
            theParentList.add(parent);
        }

        BaseResponse<List<WechatArticle>> response = new BaseResponse<List<WechatArticle>>();
        response.setSuccess(true);
        response.setMsg("" + theParentList.size());
        response.setData(theParentList);
        return response;
    }

    @ServiceMethod(method = "wechatArticle.pageList", needInSession = NeedInSessionType.NO)
    public Object pageListArticles(WechatArticle request) {
        request.setParentId(0L);
        String appId = ((SimpleSession)request.getRopRequestContext().getSession()).getAttribute(RopSessionManager.KEY_APP_ID).toString();
        if (!appId.equalsIgnoreCase(MyRopClient.APP_ID)) {
            request.setAppId(appId);
        }
        List<WechatArticle> parentList = wechatArticleMapper.getListByPage(request);
        List<WechatArticle> theParentList = new ArrayList<WechatArticle>();
        WechatArticle condition = new WechatArticle();
        condition.setAppId(appId);
        for (WechatArticle parent : parentList) {
            condition.setParentId(parent.getId());
            List<WechatArticle> subList = wechatArticleMapper.getList(condition);
            parent.setSubArticleList(subList);
            theParentList.add(parent);
        }

        BaseResponse<List<WechatArticle>> response = new BaseResponse<List<WechatArticle>>();
        response.setSuccess(true);
        response.setMsg("" + wechatArticleMapper.getCount(request));
        response.setData(theParentList);
        return response;
    }

    @ServiceMethod(method = "wechatArticle.insert", needInSession = NeedInSessionType.NO)
    public Object insertArticle(WechatArticle request) {
        Date now = new Date();
        request.setCreateTime(now);
        request.setLastChangedTime(now);
        Long sessionUserId = (Long) ((SimpleSession)request.getRopRequestContext().getSession()).getAttribute(RopSessionManager.KEY_MANAGER_ID);
        request.setCreateUserId(sessionUserId);
        request.setLastChangedUserId(sessionUserId);
        Integer count = wechatArticleMapper.insertOne(request);

        BaseResponse<Integer> response = new BaseResponse<Integer>();
        response.setSuccess(true);
        response.setData(count);
        return response;
    }

    @ServiceMethod(method = "wechatArticle.update", needInSession = NeedInSessionType.NO)
    public Object updateMenu(WechatArticle request) {
        request.setLastChangedTime(new Date());
        Long sessionUserId = (Long) ((SimpleSession)request.getRopRequestContext().getSession()).getAttribute(RopSessionManager.KEY_MANAGER_ID);
        request.setLastChangedUserId(sessionUserId);
        Integer count = wechatArticleMapper.updateOne(request);

        BaseResponse<Integer> response = new BaseResponse<Integer>();
        response.setSuccess(true);
        response.setData(count);
        return response;
    }
}
